//
//  SettingsMainViewController.m
//  Voxper
//
//  Created by Irfan Bashir on 4/28/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SettingsMainViewController.h"
#import "VoxperAppDelegate.h"
#import "AboutViewController.h"
#import "HelpViewController.h"
#import "UIManager.h"
#import "SipManager.h"
#import "Settings.h"

@implementation SettingsMainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        app = (VoxperAppDelegate *)[UIApplication sharedApplication].delegate;
    }
    return self;
}

- (void)dealloc
{
    [m_tableView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)loadView {
    m_btnUpdateGSM = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [m_btnUpdateGSM setFrame:CGRectMake(10, 10, 300, 42)];
    [m_btnUpdateGSM setTitle:@"Update GSM Number" forState:UIControlStateNormal];
    
    m_txtUpdateGSM = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 300, 45)];
    m_txtUpdateGSM.text = [app.uiManager.settings getGSMNumber];
    m_txtUpdateGSM.delegate = self;
    m_txtUpdateGSM.placeholder = @"GSM Number";
    m_txtUpdateGSM.keyboardType = UIKeyboardTypePhonePad;
    [super loadView];
    //m_txtUpdateGSM.text = [setti
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [m_tableView release];
    m_tableView = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)clearRecentCalls:(id)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Clear All Recents" otherButtonTitles:nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    [actionSheet showInView:[self view]];
    [actionSheet release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    switch (buttonIndex) {
        case 0:
            [app.uiManager clearRecentCalls];
            break;
            
        default:
            break;
    }
}

#pragma mark -
#pragma mark TableView delegates

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if ([m_txtUpdateGSM isFirstResponder])
        [m_txtUpdateGSM resignFirstResponder];
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	return indexPath;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 2;
        case 1:
            return 1;
        case 2:
            return 0;
        case 3:
            return 1;
        case 4:
            return 1;
        case 5:
            return 1;
        case 6:
            return 1;
    }
	return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 7;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return @"GSM#";
        case 1:
            return @"VN#";
        case 2:
            return nil;//@"Sip parameters";
        case 3:
            return @"Call History";
        case 4:
            return @"";
        case 5:
            return @"";
    }
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *CellIdentifier = @"RegisterStep1CellIdent";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:
                    [cell.contentView addSubview:m_txtUpdateGSM];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    break;
                case 1:
                    cell.textLabel.text = @"Update GSM Number";
            }
            break;
        case 1:
            cell.textLabel.text = [app.uiManager.settings getUserName];
            break;
        case 2:
            switch (indexPath.row) {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
            }
            break;
        case 3:
            cell.textLabel.text = @"Clear call history";
            break;
        case 4:
            cell.textLabel.text = @"About";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            break;
        case 5:
            cell.textLabel.text = @"Help";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            break;
        case 6:
            cell.textLabel.text = @"Disconnect";
            break;
    }
	return cell;
}

- (void)tableView:(UITableView *)_tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[_tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:
                    break;
                case 1:
                    break;
            }
            break;
        case 1:
            break;
        case 2:
            switch (indexPath.row) {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
            }
            break;
        case 3:
            [self clearRecentCalls:nil];
            break;
        case 4:
        {
            AboutViewController *aboutViewController = [[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:[NSBundle mainBundle]];
            [[self navigationController] pushViewController:aboutViewController animated:YES];
            [aboutViewController release];
        }
            break;
        case 5:
        {
            HelpViewController *helpViewController = [[HelpViewController alloc] initWithNibName:@"HelpViewController" bundle:[NSBundle mainBundle]];
            [[self navigationController] pushViewController:helpViewController animated:YES];
            
            [helpViewController release];
        }
            break;
        case 6:
            [[app manager] disconnect];
            [[[app uiManager] settings] setDeviceToken:@""];
            [[app uiManager] sendDeviceToken];
            break;
    }
}
@end
